package com.yupi.mybi.bimq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DlxConsumer {

    @RabbitListener(queues = BiConstant.DLX_QUEUE)
    public void handleDeadLetterMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag) {
        try {
            log.error("收到死信消息: {}", message);
            // 这里可以记录日志、发送告警或人工干预
            // 例如：将失败任务记录到数据库
            // 确认消息已处理
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            log.error("死信消息处理失败: {}", e.getMessage());
        }
    }
}